package com.xbongbong.paas.controller.mobile;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageSendToConversationDTO;
import com.xbongbong.pro.message.pojo.vo.MessageSendToConversationVO;
import com.xbongbong.pro.message.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 移动段右上角分享给同事
 * @author 魏荣杰
 * @date 2019/5/20 14:19
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/common/message")
public class MobileMessageController {
    private static final Logger LOG = LoggerFactory.getLogger(MobileMessageController.class);

    @Resource
    private MessageService messageService;

    @RequestMapping(value = "/send", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String send(@RequestBody @Valid MessageSendToConversationDTO messageSendToConversationDTO, BindingResult br) {
        XbbResponse<MessageSendToConversationVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.sendToConversation(messageSendToConversationDTO);
                MessageSendToConversationVO messageSendToConversationVO = new MessageSendToConversationVO();
                response = new XbbResponse<>(messageSendToConversationVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
